package com.junqi.sys.application.controller.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.base.Preconditions;
import com.junqi.common.entity.AjaxResult;
import com.junqi.sys.application.controller.convert.SysUserDtoConvert;
import com.junqi.sys.domain.bo.SysUserBo;
import com.junqi.sys.domain.service.SysUserDomainService;
import com.junqi.sys.entity.SysUserDto;
import com.junqi.sys.infra.basic.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    SysUserDomainService sysUserDomainService;

    /**
     * 用户登录
     * @param code
     * @return
     */
    @RequestMapping("/doLogin")
    public AjaxResult doLogin(@RequestParam("code") String code) {
        try {
            Preconditions.checkArgument(StrUtil.isNotBlank(code), "验证码不能为空!");
            SaTokenInfo saTokenInfo = sysUserDomainService.doLogin(code);
            return AjaxResult.success(saTokenInfo);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 用户退出
     */
    @RequestMapping("/logOut")
    public AjaxResult logOut() {
        try {
            StpUtil.logout();
            return AjaxResult.success("已退出登录");
        } catch (Exception e) {
            return AjaxResult.error("退出登录失败");
        }
    }


    /**
     * 查询用户信息
     * @return
     */
    @RequestMapping("/getUserInfo")
    public AjaxResult getUserInfo(@RequestParam("userUniqueId") String userUniqueId){
        try {
            boolean login = StpUtil.isLogin();
            if(login){
                return AjaxResult.success(sysUserDomainService.getUserInfo(userUniqueId));
            }else{
                return AjaxResult.error("用户未登录");
            }
        } catch (Exception e) {
            return AjaxResult.error("查询不到用户信息");
        }
    }


    /**
     * 查询用户房间列表
     * @param id 房间id
     * @return
     */
    @GetMapping("/getUserRoomList")
    public AjaxResult getRoomUserList(@RequestParam("id") String id){
        List<Map<String,Object>> list=sysUserDomainService.getRoomUserList(id);
        return AjaxResult.success(list);
    }


    /**
     * openfeign接口：根据用户id查询用户信息
     * @return
     */
    @GetMapping("/getUserInfoByUserId")
    public SysUserDto getUserRoomList(@RequestParam("userId") String userId){
        try {
            SysUserBo sysUser = sysUserDomainService.getUserInfoByUserId(userId);
            return SysUserDtoConvert.INSTANCE.convertBoToDto(sysUser);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 修改用户名
     * @param userName
     * @return
     */
    @GetMapping("/updateUserName")
    public AjaxResult updateUserName(@RequestParam("userName")  String userName){
        Preconditions.checkArgument(StrUtil.isNotBlank(userName), "用户名称不能为空!");
        sysUserDomainService.updateUserName(userName);
        return AjaxResult.success();
    }

}
